<template>
  <div>
    <ul class="list">
      <li
        class="item"
        v-for="item in letters"
        :key="item"
        :ref="item"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
        @click="handleLetter(item)"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
import throttle from 'lodash/throttle'
export default {
  name: 'AlphaCity',
  props: {
    cities: Object
  },
  data() {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  computed: {
    letters() {
      const letters = []

      // ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z"]
      // eslint-disable-next-line prefer-const
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },

  mounted() {},
  updated() {
    // eslint-disable-next-line dot-notation
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetter(key) {
      console.log(key)
      this.$bus.$emit('letterChange', key)
    },
    handleTouchStart() {
      this.touchStatus = true
    },
    handleTouchMove: throttle(function (e) {
      if (this.touchStatus) {
        // eslint-disable-next-line dot-notation

        const touchY = e.touches[0].clientY
        const index = Math.floor((touchY - this.startY) / 20)
        console.log(index)
        this.$bus.$emit('letterChange', this.letters[index])
      }
    }, 16),
    handleTouchEnd() {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="less" scoped>
.list {
  width: 0.4rem;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .item {
    line-height: 0.5333rem;
    text-align: center;
    color: #00bcd4;
  }
}
</style>
